package com.sun.hunk.util;

/**
 *
 * @author Administrator
 */
import java.io.*;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;

public class FileUtil {

    public static void main(String[] args) throws IOException {
        File jarFile = new File("C:\\Documents and Settings\\administrator.CONGSERVER\\Desktop\\current\\explorer_files.rar");

        for (File f : getSelectedFiles(getExtartedFiles(jarFile), "java")) {
            System.out.println("F : " + f.getName());
        }
    }

    public static List<File> getSelectedFiles(List<File> files, String ext) {
        List<File> selectedFiles = new ArrayList<File>();
        for (File file : files) {
            if (file != null) {
                if (file.getName().endsWith("." + ext)) {
                    selectedFiles.add(file);
                }
            }
        }
        return selectedFiles;
    }

    public static List<File> getExtartedFiles(File jarFile) throws IOException {
        List<File> files = new ArrayList<File>();
        JarFile jar = new JarFile(jarFile);
        Enumeration enums = jar.entries();
        String destDir = jarFile.getParent();
        while (enums.hasMoreElements()) {
            JarEntry file = (JarEntry) enums.nextElement();
            File f = new File(destDir + File.separator + file.getName());
            files.add(f);
            if (file.isDirectory()) { // if its a directory, create it
                f.mkdir();
                continue;
            }
            InputStream is = jar.getInputStream(file); // get the input stream
            FileOutputStream fos = new FileOutputStream(f);
            while (is.available() > 0) {  // write contents of 'is' to 'fos'
                fos.write(is.read());
            }
            fos.close();
            is.close();
        }
        return files;
    }

    public static void InputStreamToFile(InputStream inputStream, File outputFile) {
        try {
            OutputStream out = new FileOutputStream(outputFile);
            byte buf[] = new byte[1024];
            int len;
            while ((len = inputStream.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.close();
            inputStream.close();
        } catch (IOException e) {
        }
    }

    public static byte[] getBytes(File file) {
        if (file == null) {
            return null;
        }
        byte[] bFile = new byte[(int) file.length()];

        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            //convert file into array of bytes
            fileInputStream.read(bFile);
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bFile;
    }

    public void createIfNotExist(File file) {
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    public static String getTempFolder() throws IOException {
        String property = "java.io.tmpdir";
        String temp = System.getProperty(property);
        File file = new File(temp, "ENHANCE");
        if (!file.exists()) {
            file.mkdir();
        }
        return file.getAbsolutePath();
    }

    public static String getReportFolder() throws IOException {
        File file = new File(getTempFolder(), "REPORT");
        if (!file.exists()) {
            file.mkdir();
        }
        return file.getAbsolutePath();
    }
}
